﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataStore.Domain;
using Infrastructure.CurrentData;
using Infrastructure.ViewModels;
using Microsoft.Practices.Composite.Events;

namespace UI.Controls.Dictionaries
{
    /// <summary>
    /// Interaction logic for ConditionGrid.xaml
    /// </summary>
    public partial class ConditionGrid : UserControl, IDictionaryGrid
    {
        AllConditionsViewModel _allItems;
        readonly IEventAggregator _eventAggregator;

        public ConditionGrid()
        {
            InitializeComponent();

            _eventAggregator = Current.Container.Resolve<IEventAggregator>();
            LoadItems();
        }

        /// <summary>
        /// Loads the publishers into the grid.
        /// </summary>
        void LoadItems()
        {
            _allItems = new AllConditionsViewModel(_eventAggregator);
            Items = _allItems.PublishingFormats;
            _allItems.Loaded += delegate
            {
                SetSelectedItem();
            };
        }

        /// <summary>
        /// Selection changed event
        /// </summary>
        public SelectionChangedEventHandler SelectionChanged;

        /// <summary>
        /// Called when [selection changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        protected void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectionChanged != null)
            {
                SelectionChanged(sender, e);
            }
        }

        #region Implementation of IDictionaryGrid

        /// <summary>
        /// Gets or sets the grid items.
        /// </summary>
        /// <value>The items.</value>
        public IEnumerable Items
        {
            get
            {
                return DataContext as IEnumerable;
            }
            set
            {
                DataContext = value;
                Conditions.ItemsSource = value;
            }
        }

        Condition _selected;
        /// <summary>
        /// Gets or sets the selected item.
        /// </summary>
        /// <value>The selected item.</value>
        public object Selected
        {
            get
            {
                return _selected;
            }
            set
            {
                _selected = value as Condition;
                SetSelectedItem();
            }
        }

        void SetSelectedItem()
        {
            if (Selected != null)
            {
                foreach (Condition publisher in Conditions.Items)
                {

                    if (publisher.Id == _selected.Id)
                    {
                        Conditions.SelectedItem = publisher;
                    }
                }
            }
        }

        /// <summary>
        /// Updates the grid after edit operation.
        /// </summary>
        /// <param name="operation">The operation.</param>
        /// <param name="item">The item.</param>
        public void UpdateGrid(UpdateOperation operation, object item)
        {
            LoadItems();
        }

        #endregion

        /// <summary>
        /// Handles the SelectionChanged event of the Publishers control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void Conditions_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _selected = Conditions.SelectedItem as Condition;
            OnSelectionChanged(sender, e);
        }
    }
}
